package com.demo.juc.cas;

/**
 * @Author: yaoshubin
 * @Date: 2023-07-20  19:22
 * @Description: 测试 Number 线程非安全
 */
public class DemoNumber {

     private volatile int number = 0;


    public static void main(String[] args) {
        DemoNumber demoNumber = new DemoNumber();
        System.out.println(demoNumber.getNumber());
    }

    public int getNumber() {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                number++;
            }, "线程：" + i).start();
        }
        return number;
    }

    public synchronized void setNumber(int number) {
        this.number = number;
    }
}
